package com.fr.chart;

import com.fr.base.CodeUtils;
import com.fr.base.FRContext;
import com.fr.base.StringUtils;
import com.fr.base.Style;
import com.fr.base.core.GraphHelper;
import com.fr.base.core.UUID;
import com.fr.base.core.html.Tag;
import com.fr.base.core.json.JSONArray;
import com.fr.base.core.json.JSONException;
import com.fr.base.core.json.JSONObject;
import com.fr.base.core.util.vt.VT4FR;
import com.fr.base.core.util.vt.VT4FR.FUNC;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLReadable;
import com.fr.base.xml.XMLableReader;
import com.fr.chart.base.equals.Equals;
import com.fr.chart.core.glyph.ChartGlyph;
import com.fr.chart.core.glyph.PlotGlyph;
import com.fr.chart.plot.DataPoint;
import com.fr.chart.plot.DataSeries;
import com.fr.dialog.NameObject;
import com.fr.report.FloatElement;
import com.fr.report.ResultWorkBook;
import com.fr.report.cellElement.Formula;
import com.fr.report.cellElement.core.CellUtils;
import com.fr.report.core.PaintUtils;
import com.fr.report.js.JavaScript;
import com.fr.report.js.NameJavaScript;
import com.fr.report.js.NameJavaScriptGroup;
import com.fr.report.js.ReportletHyperlink;
import com.fr.report.js.WebHyperlink;
import com.fr.report.parameter.Parameter;
import com.fr.report.script.Calculator;
import com.fr.web.Browser;
import com.fr.web.Repository;
import com.fr.web.core.HTMLWriterUtils;
import com.fr.web.core.SessionIDInfor;
import com.fr.web.core.service.ChartHtmlService;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.geom.Rectangle2D.Double;
import java.awt.geom.RoundRectangle2D.Double;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ChartPainter extends ChartLayout
{
  private static final long serialVersionUID = 7601506246912210545L;
  public static final String XML_TAG = "CP";
  private List nameObjectList = new ArrayList();

  public void addGlyph(ChartGlyph paramChartGlyph)
  {
    addNamedGlyph("Default", paramChartGlyph);
  }

  public void addNamedGlyph(String paramString, ChartGlyph paramChartGlyph)
  {
    this.nameObjectList.add(new NameObject(paramString, paramChartGlyph));
  }

  public int getGlyphSize()
  {
    return this.nameObjectList.size();
  }

  public String getGlyphName(int paramInt)
  {
    return ((NameObject)this.nameObjectList.get(paramInt)).getName();
  }

  public ChartGlyph getGlyph(int paramInt)
  {
    return ((ChartGlyph)((NameObject)this.nameObjectList.get(paramInt)).getObject());
  }

  public void removeAllGlyphs()
  {
    this.nameObjectList.clear();
  }

  public void paint(Graphics paramGraphics, int paramInt1, int paramInt2, Style paramStyle)
  {
    paintIndexGlyph(paramGraphics, paramInt1, paramInt2, paramStyle, getSelectedIndex());
  }

  private void paintIndexGlyph(Graphics paramGraphics, int paramInt1, int paramInt2, Style paramStyle, int paramInt3)
  {
    if ((paramInt1 == 0) || (paramInt2 == 0))
      return;
    Graphics2D localGraphics2D = (Graphics2D)paramGraphics;
    Object localObject = localGraphics2D.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
    localGraphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    if (getGlyphSize() > 0)
    {
      int i = Math.min(Math.max(0, paramInt3), getGlyphSize() - 1);
      ChartGlyph localChartGlyph = getGlyph(i);
      if (localChartGlyph.isRoundBorder())
        localChartGlyph.setBounds(new RoundRectangle2D.Double(0.0D, 0.0D, paramInt1, paramInt2, 10.0D, 10.0D));
      else
        localChartGlyph.setBounds(new Rectangle2D.Double(0.0D, 0.0D, paramInt1, paramInt2));
      localChartGlyph.draw(paramGraphics);
    }
    localGraphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, localObject);
  }

  public Image paint4Float(FloatElement paramFloatElement, ChartGlyph paramChartGlyph, int paramInt1, int paramInt2)
  {
    BufferedImage localBufferedImage = GraphHelper.createBufferedImage(paramInt1, paramInt2, 6);
    Graphics2D localGraphics2D = localBufferedImage.createGraphics();
    localGraphics2D.setPaint(Color.white);
    PaintUtils.paintBackground(localGraphics2D, paramFloatElement.getStyle(), paramInt1 - 1, paramInt2 - 1);
    if (paramChartGlyph != null)
    {
      if (paramChartGlyph.isRoundBorder())
        paramChartGlyph.setBounds(new RoundRectangle2D.Double(0.0D, 0.0D, paramInt1 - 1, paramInt2 - 1, 10.0D, 10.0D));
      else
        paramChartGlyph.setBounds(new Rectangle2D.Double(0.0D, 0.0D, paramInt1 - 1, paramInt2 - 1));
      paramChartGlyph.draw(localGraphics2D);
    }
    PaintUtils.paintBorder(localGraphics2D, paramFloatElement.getStyle(), paramInt1 - 1, paramInt2 - 1);
    localBufferedImage.flush();
    localGraphics2D.dispose();
    return localBufferedImage;
  }

  public void paintTag(Repository paramRepository, int paramInt1, int paramInt2, Style paramStyle, Tag paramTag)
  {
    paintTag(paramRepository, paramInt1, paramInt2, paramStyle, paramTag, null);
  }

  public void paintTag(Repository paramRepository, int paramInt1, int paramInt2, Style paramStyle, Tag paramTag, FloatElement paramFloatElement)
  {
    Object localObject1;
    Object localObject2;
    Object localObject3;
    if ((getGlyphSize() == 1) || (!(VT4FR.ADVANCED_CHART.support())))
    {
      localObject1 = getGlyph(0);
      localObject2 = ((ChartGlyph)localObject1).getPlotGlyph();
      if ((((PlotGlyph)localObject2).supportJSDrawing()) && (((ChartGlyph)localObject1).isJSDraw()) && (VT4FR.DYNAMIC_CHART.support()))
      {
        paramTag.sub(getChartGlyphTag(paramRepository, (ChartGlyph)localObject1, paramInt1, paramInt2));
        return;
      }
      Image localImage1 = null;
      if (paramFloatElement == null)
        localImage1 = CellUtils.value2Image(this, paramStyle, paramInt1, paramInt2);
      else
        localImage1 = paint4Float(paramFloatElement, (ChartGlyph)localObject1, paramInt1, paramInt2);
      Tag localTag = paramRepository.checkoutImageTag(localImage1, paramInt1, paramInt2);
      paramTag.sub(localTag);
      if ((localObject2 != null) && (VT4FR.ADVANCED_CHART.support()) && (((((PlotGlyph)localObject2).getHotHyperLink() != null) || (StringUtils.isNotEmpty(((PlotGlyph)localObject2).getHotTooltip())))))
      {
        localObject3 = "chart_hotmap_" + ((ChartGlyph)localObject1).getPlotGlyph().hashCode();
        localTag.attr("usemap", "#" + ((String)localObject3));
        paramTag.sub(dealChartHot((ChartGlyph)localObject1, (String)localObject3, paramRepository));
      }
    }
    else if (getGlyphSize() > 1)
    {
      localObject1 = new JSONObject();
      try
      {
        localObject2 = new JSONArray();
        int i = 0;
        int j = getGlyphSize();
        while (i < j)
        {
          localObject3 = new JSONObject();
          ((JSONObject)localObject3).put("name", getGlyphName(i));
          ChartGlyph localChartGlyph = getGlyph(i);
          if ((localChartGlyph.isJSDraw()) && (localChartGlyph.getPlotGlyph().supportJSDrawing()))
          {
            ((JSONObject)localObject3).put("url", makeChartGlyphURL(paramRepository, localChartGlyph, paramInt1, paramInt2));
            ((JSONObject)localObject3).put("isJS", true);
          }
          else
          {
            Image localImage2 = null;
            if (paramFloatElement == null)
              localImage2 = localChartGlyph.toImage(paramInt1, paramInt2);
            else
              localImage2 = paint4Float(paramFloatElement, localChartGlyph, paramInt1, paramInt2);
            ((JSONObject)localObject3).put("url", paramRepository.checkoutImage(localImage2));
            PlotGlyph localPlotGlyph = localChartGlyph.getPlotGlyph();
            if ((localPlotGlyph != null) && (VT4FR.ADVANCED_CHART.support()) && (((localPlotGlyph.getHotHyperLink() != null) || (StringUtils.isNotEmpty(localPlotGlyph.getHotTooltip())))))
            {
              String str2 = "chart_hotmap_" + localChartGlyph.getPlotGlyph().hashCode();
              ((JSONObject)localObject3).put("usemap", "#" + str2);
              ((JSONObject)localObject3).put("mapHtml", dealChartHot(localChartGlyph, str2, paramRepository).toHtml());
            }
          }
          ((JSONArray)localObject2).put(localObject3);
          ++i;
        }
        if (paramFloatElement != null)
          ((JSONObject)localObject1).put("isfloat", true);
        ((JSONObject)localObject1).put("items", localObject2);
        ((JSONObject)localObject1).put("type", "simplechart");
        ((JSONObject)localObject1).put("chartWidth", paramInt1);
        ((JSONObject)localObject1).put("chartHeight", paramInt2);
        ((JSONObject)localObject1).put("selectedIndex", getSelectedIndex());
        String str1 = UUID.randomUUID().toString();
        ((JSONObject)localObject1).put("chartPaintID", str1);
        HashMap localHashMap = new HashMap();
        localHashMap.put("chartPaint", this);
        ChartHtmlService.addMap2List(localHashMap, str1);
      }
      catch (JSONException localJSONException)
      {
        FRContext.getLogger().log(Level.WARNING, localJSONException.getMessage(), localJSONException);
      }
      paramTag.attr("widget", ((JSONObject)localObject1).toString());
    }
  }

  private Tag getChartGlyphTag(Repository paramRepository, ChartGlyph paramChartGlyph, int paramInt1, int paramInt2)
  {
    Tag localTag = new Tag("iframe").attr("SRC", makeChartGlyphURL(paramRepository, paramChartGlyph, paramInt1, paramInt2)).css("width", paramInt1 + "px").css("height", paramInt2 + "px").attr("scrolling", "no").attr("marginheight", "0").attr("marginwidth", "0").attr("allowTransparency", "true").attr("frameborder", "0");
    return localTag;
  }

  private String makeChartGlyphURL(Repository paramRepository, ChartGlyph paramChartGlyph, double paramDouble1, double paramDouble2)
  {
    String str1 = UUID.randomUUID().toString();
    JSONObject localJSONObject = null;
    try
    {
      localJSONObject = paramChartGlyph.toJSONObject();
    }
    catch (JSONException localJSONException)
    {
      localJSONException.printStackTrace();
    }
    String str2 = "";
    PlotGlyph localPlotGlyph = paramChartGlyph.getPlotGlyph();
    if ((localPlotGlyph != null) && (((localPlotGlyph.getHotHyperLink() != null) || (StringUtils.isNotEmpty(localPlotGlyph.getHotTooltip())))))
    {
      paramChartGlyph.toImage((int)paramDouble1, (int)paramDouble2);
      localObject = paramRepository.checkoutImageTag(GraphHelper.createBufferedImage((int)paramDouble1, (int)paramDouble2, 6), (int)paramDouble1, (int)paramDouble2);
      String str3 = "chart_hotmap_" + paramChartGlyph.getPlotGlyph().hashCode();
      ((Tag)localObject).attr("usemap", "#" + str3);
      str2 = ((Tag)localObject).toString() + dealChartHot(paramChartGlyph, str3, paramRepository).toString();
    }
    Object localObject = new HashMap();
    ((Map)localObject).put("chartAttr", localJSONObject.toString());
    ((Map)localObject).put("width", String.valueOf(paramDouble1));
    ((Map)localObject).put("height", String.valueOf(paramDouble2));
    ((Map)localObject).put("id", str1);
    ((Map)localObject).put("img", str2);
    ChartHtmlService.addMap2List((Map)localObject, str1);
    return ((String)paramRepository.getServletURL() + "?op=charthtml&chartID=" + str1);
  }

  private Tag dealChartHot(ChartGlyph paramChartGlyph, String paramString, Repository paramRepository)
  {
    PlotGlyph localPlotGlyph = paramChartGlyph.getPlotGlyph();
    Tag localTag1 = new Tag("map");
    localTag1.attr("name", paramString);
    localTag1.attr("id", paramString);
    int i = 0;
    int j = 0;
    int k = localPlotGlyph.getSeriesSize();
    while (j < k)
    {
      DataSeries localDataSeries = localPlotGlyph.getSeries(j);
      for (int l = 0; l < localDataSeries.getDataPointCount(); ++l)
      {
        DataPoint localDataPoint = localDataSeries.getDataPoint(l);
        Tag localTag2 = localDataPoint.getImageMapAreaTag(paramChartGlyph);
        if (localTag2 != null)
        {
          localTag1.sub(localTag2);
          localTag2.attr("id", i + "");
          if (localPlotGlyph.getHotHyperLink() != null)
            dealChartHotHyperLink(localPlotGlyph.getHotHyperLink(), localDataPoint, paramRepository, localTag2);
          if (localPlotGlyph.getHotTooltip() != null)
            dealChartHotTooltip(localPlotGlyph.getHotTooltip(), localDataPoint, paramRepository, localTag2);
          ++i;
        }
      }
      ++j;
    }
    return localTag1;
  }

  private void dealChartHotHyperLink(NameJavaScriptGroup paramNameJavaScriptGroup, DataPoint paramDataPoint, Repository paramRepository, Tag paramTag)
  {
    if (paramNameJavaScriptGroup != null)
    {
      NameJavaScriptGroup localNameJavaScriptGroup = null;
      try
      {
        localNameJavaScriptGroup = (NameJavaScriptGroup)paramNameJavaScriptGroup.clone();
      }
      catch (CloneNotSupportedException localCloneNotSupportedException)
      {
      }
      int i = 0;
      int j = localNameJavaScriptGroup.size();
      while (i < j)
      {
        Parameter[] arrayOfParameter;
        JavaScript localJavaScript = localNameJavaScriptGroup.getNameHyperlink(i).getJavaScript();
        if (localJavaScript instanceof ReportletHyperlink)
        {
          arrayOfParameter = ((ReportletHyperlink)localJavaScript).getParameters();
          dealParameter4Hyperlink(arrayOfParameter, paramDataPoint, paramRepository);
        }
        if (localJavaScript instanceof WebHyperlink)
        {
          arrayOfParameter = ((WebHyperlink)localJavaScript).getParameters();
          dealParameter4Hyperlink(arrayOfParameter, paramDataPoint, paramRepository);
        }
        ++i;
      }
      try
      {
        paramRepository.getBrowser().setHrefOnArea(paramTag);
        paramTag.attr("onclick", "FR.doHyperlink(event||window.event, " + HTMLWriterUtils.writeJSLinkContent(localNameJavaScriptGroup, paramRepository) + ", true)");
      }
      catch (JSONException localJSONException)
      {
      }
    }
  }

  private void dealParameter4Hyperlink(Parameter[] paramArrayOfParameter, DataPoint paramDataPoint, Repository paramRepository)
  {
    int i;
    if (paramArrayOfParameter != null)
      for (i = 0; i < paramArrayOfParameter.length; ++i)
      {
        Parameter localParameter = paramArrayOfParameter[i];
        Object localObject1 = localParameter.getValue();
        if (localObject1 instanceof Formula)
        {
          Object localObject2;
          try
          {
            String str = ((Formula)localObject1).getContent();
            if (str.startsWith("="))
              str = str.substring(1);
            localObject2 = paramDataPoint.getCalculator();
            if (paramRepository.getSessionIDInfor() != null)
            {
              ((Calculator)localObject2).setCurrentReport(paramRepository.getSessionIDInfor().getWorkBook2Show().getReport(0));
              ((Calculator)localObject2).setCurrentTableDataSource(paramRepository.getSessionIDInfor().getContextBook());
            }
            localParameter.setValue(((Calculator)localObject2).eval(str).toString());
          }
          catch (Exception localException)
          {
            localObject2 = "error happens at calculating formula";
            throw new RuntimeException((String)localObject2, localException);
          }
        }
      }
  }

  private void dealChartHotTooltip(String paramString, DataPoint paramDataPoint, Repository paramRepository, Tag paramTag)
  {
    String str = "";
    try
    {
      str = paramDataPoint.getCalculator().renderTpl(paramString);
    }
    catch (Exception localException)
    {
      str = paramString;
    }
    paramTag.attr("title", CodeUtils.htmlEncode(str));
  }

  public Object clone()
    throws CloneNotSupportedException
  {
    ChartPainter localChartPainter = (ChartPainter)super.clone();
    if (this.nameObjectList != null)
    {
      localChartPainter.nameObjectList = new ArrayList();
      int i = 0;
      int j = getGlyphSize();
      while (i < j)
      {
        localChartPainter.nameObjectList.add(new NameObject(getGlyphName(i), (ChartGlyph)getGlyph(i).clone()));
        ++i;
      }
    }
    return localChartPainter;
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
    int i;
    paramXMLPrintWriter.startTAG("CP");
    super.writeXML(paramXMLPrintWriter);
    if ((this.nameObjectList != null) && (getGlyphSize() > 0))
      for (i = 0; i < this.nameObjectList.size(); ++i)
      {
        paramXMLPrintWriter.startTAG("NameGlyph");
        paramXMLPrintWriter.startTAG("Name").attr("value", getGlyphName(i)).end();
        getGlyph(i).writeXML(paramXMLPrintWriter);
        paramXMLPrintWriter.end();
      }
    paramXMLPrintWriter.end();
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
    super.readXML(paramXMLableReader);
    if (paramXMLableReader.isChildNode())
    {
      String str = paramXMLableReader.getTagName();
      if (str.equals("NameGlyph"))
        paramXMLableReader.readXMLObject(new XMLReadable(this)
        {
          String glyphName;
          ChartGlyph glyph;
          private final ChartPainter this$0;

          public void readXML()
          {
            if (paramXMLableReader.isChildNode())
            {
              String str1 = paramXMLableReader.getTagName();
              if (str1.equals("Name"))
              {
                String str2;
                if ((str2 = paramXMLableReader.getAttr("value")) != null)
                  this.glyphName = str2;
              }
              if (str1.equals("ChartGlyph"))
                this.glyph = ((ChartGlyph)paramXMLableReader.readXMLObject(new ChartGlyph()));
              if ((this.glyphName != null) && (this.glyph != null))
                this.this$0.addNamedGlyph(this.glyphName, this.glyph);
            }
          }
        });
    }
  }

  public boolean equals(Object paramObject)
  {
    if (!(paramObject instanceof ChartPainter))
      return false;
    ChartPainter localChartPainter = (ChartPainter)paramObject;
    if (!(super.equals(localChartPainter)))
      return false;
    if (localChartPainter.getGlyphSize() != getGlyphSize())
      return false;
    for (int i = 0; i < localChartPainter.getGlyphSize(); ++i)
      if (!(Equals.equals(getGlyph(i), localChartPainter.getGlyph(i))))
        return false;
    return true;
  }
}